set(ODBC_SOURCE_DIR ${CMAKE_SOURCE_DIR}/contrib/unixodbc)
set(ODBC_BINARY_DIR ${CMAKE_BINARY_DIR}/contrib/unixodbc)


set(SRCS
${ODBC_SOURCE_DIR}/libltdl/lt__alloc.c
${ODBC_SOURCE_DIR}/libltdl/lt__strl.c
${ODBC_SOURCE_DIR}/libltdl/ltdl.c
${ODBC_SOURCE_DIR}/libltdl/lt_dlloader.c
${ODBC_SOURCE_DIR}/libltdl/slist.c
${ODBC_SOURCE_DIR}/libltdl/lt_error.c
${ODBC_SOURCE_DIR}/libltdl/loaders/dlopen.c
${ODBC_SOURCE_DIR}/libltdl/loaders/preopen.c
#${ODBC_SOURCE_DIR}/libltdl/lt__dirent.c
#${ODBC_SOURCE_DIR}/libltdl/lt__argz.c
#${ODBC_SOURCE_DIR}/libltdl/loaders/dld_link.c
#${ODBC_SOURCE_DIR}/libltdl/loaders/load_add_on.c
#${ODBC_SOURCE_DIR}/libltdl/loaders/shl_load.c
#${ODBC_SOURCE_DIR}/libltdl/loaders/loadlibrary.c
#${ODBC_SOURCE_DIR}/libltdl/loaders/dyld.c

# This file is generated by 'libtool' inside libltdl directory and then removed.
${CMAKE_CURRENT_SOURCE_DIR}/linux_x86_64/libltdl/libltdlcS.c
)

add_library(ltdl ${LINK_MODE} ${SRCS})

target_include_directories(ltdl PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/linux_x86_64/libltdl)
target_include_directories(ltdl PUBLIC ${ODBC_SOURCE_DIR}/libltdl)
target_include_directories(ltdl PUBLIC ${ODBC_SOURCE_DIR}/libltdl/libltdl)

target_compile_definitions(ltdl PRIVATE -DHAVE_CONFIG_H -DLTDL -DLTDLOPEN=libltdlc)

target_compile_options(ltdl PRIVATE -Wno-constant-logical-operand -Wno-unknown-warning-option -O2)


set(SRCS
${ODBC_SOURCE_DIR}/DriverManager/__attribute.c
${ODBC_SOURCE_DIR}/DriverManager/__connection.c
${ODBC_SOURCE_DIR}/DriverManager/__handles.c
${ODBC_SOURCE_DIR}/DriverManager/__info.c
${ODBC_SOURCE_DIR}/DriverManager/__stats.c
${ODBC_SOURCE_DIR}/DriverManager/SQLAllocConnect.c
${ODBC_SOURCE_DIR}/DriverManager/SQLAllocEnv.c
${ODBC_SOURCE_DIR}/DriverManager/SQLAllocHandle.c
${ODBC_SOURCE_DIR}/DriverManager/SQLAllocHandleStd.c
${ODBC_SOURCE_DIR}/DriverManager/SQLAllocStmt.c
${ODBC_SOURCE_DIR}/DriverManager/SQLBindCol.c
${ODBC_SOURCE_DIR}/DriverManager/SQLBindParam.c
${ODBC_SOURCE_DIR}/DriverManager/SQLBindParameter.c
${ODBC_SOURCE_DIR}/DriverManager/SQLBrowseConnect.c
${ODBC_SOURCE_DIR}/DriverManager/SQLBrowseConnectW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLBulkOperations.c
${ODBC_SOURCE_DIR}/DriverManager/SQLCancel.c
${ODBC_SOURCE_DIR}/DriverManager/SQLCancelHandle.c
${ODBC_SOURCE_DIR}/DriverManager/SQLCloseCursor.c
${ODBC_SOURCE_DIR}/DriverManager/SQLColAttribute.c
${ODBC_SOURCE_DIR}/DriverManager/SQLColAttributes.c
${ODBC_SOURCE_DIR}/DriverManager/SQLColAttributesW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLColAttributeW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLColumnPrivileges.c
${ODBC_SOURCE_DIR}/DriverManager/SQLColumnPrivilegesW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLColumns.c
${ODBC_SOURCE_DIR}/DriverManager/SQLColumnsW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLConnect.c
${ODBC_SOURCE_DIR}/DriverManager/SQLConnectW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLCopyDesc.c
${ODBC_SOURCE_DIR}/DriverManager/SQLDataSources.c
${ODBC_SOURCE_DIR}/DriverManager/SQLDataSourcesW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLDescribeCol.c
${ODBC_SOURCE_DIR}/DriverManager/SQLDescribeColW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLDescribeParam.c
${ODBC_SOURCE_DIR}/DriverManager/SQLDisconnect.c
${ODBC_SOURCE_DIR}/DriverManager/SQLDriverConnect.c
${ODBC_SOURCE_DIR}/DriverManager/SQLDriverConnectW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLDrivers.c
${ODBC_SOURCE_DIR}/DriverManager/SQLDriversW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLEndTran.c
${ODBC_SOURCE_DIR}/DriverManager/SQLError.c
${ODBC_SOURCE_DIR}/DriverManager/SQLErrorW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLExecDirect.c
${ODBC_SOURCE_DIR}/DriverManager/SQLExecDirectW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLExecute.c
${ODBC_SOURCE_DIR}/DriverManager/SQLExtendedFetch.c
${ODBC_SOURCE_DIR}/DriverManager/SQLFetch.c
${ODBC_SOURCE_DIR}/DriverManager/SQLFetchScroll.c
${ODBC_SOURCE_DIR}/DriverManager/SQLForeignKeys.c
${ODBC_SOURCE_DIR}/DriverManager/SQLForeignKeysW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLFreeConnect.c
${ODBC_SOURCE_DIR}/DriverManager/SQLFreeEnv.c
${ODBC_SOURCE_DIR}/DriverManager/SQLFreeHandle.c
${ODBC_SOURCE_DIR}/DriverManager/SQLFreeStmt.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetConnectAttr.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetConnectAttrW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetConnectOption.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetConnectOptionW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetCursorName.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetCursorNameW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetData.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetDescField.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetDescFieldW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetDescRec.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetDescRecW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetDiagField.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetDiagFieldW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetDiagRec.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetDiagRecW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetEnvAttr.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetFunctions.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetInfo.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetInfoW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetStmtAttr.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetStmtAttrW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetStmtOption.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetTypeInfo.c
${ODBC_SOURCE_DIR}/DriverManager/SQLGetTypeInfoW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLMoreResults.c
${ODBC_SOURCE_DIR}/DriverManager/SQLNativeSql.c
${ODBC_SOURCE_DIR}/DriverManager/SQLNativeSqlW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLNumParams.c
${ODBC_SOURCE_DIR}/DriverManager/SQLNumResultCols.c
${ODBC_SOURCE_DIR}/DriverManager/SQLParamData.c
${ODBC_SOURCE_DIR}/DriverManager/SQLParamOptions.c
${ODBC_SOURCE_DIR}/DriverManager/SQLPrepare.c
${ODBC_SOURCE_DIR}/DriverManager/SQLPrepareW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLPrimaryKeys.c
${ODBC_SOURCE_DIR}/DriverManager/SQLPrimaryKeysW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLProcedureColumns.c
${ODBC_SOURCE_DIR}/DriverManager/SQLProcedureColumnsW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLProcedures.c
${ODBC_SOURCE_DIR}/DriverManager/SQLProceduresW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLPutData.c
${ODBC_SOURCE_DIR}/DriverManager/SQLRowCount.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSetConnectAttr.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSetConnectAttrW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSetConnectOption.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSetConnectOptionW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSetCursorName.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSetCursorNameW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSetDescField.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSetDescFieldW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSetDescRec.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSetEnvAttr.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSetParam.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSetPos.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSetScrollOptions.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSetStmtAttr.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSetStmtAttrW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSetStmtOption.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSetStmtOptionW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSpecialColumns.c
${ODBC_SOURCE_DIR}/DriverManager/SQLSpecialColumnsW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLStatistics.c
${ODBC_SOURCE_DIR}/DriverManager/SQLStatisticsW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLTablePrivileges.c
${ODBC_SOURCE_DIR}/DriverManager/SQLTablePrivilegesW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLTables.c
${ODBC_SOURCE_DIR}/DriverManager/SQLTablesW.c
${ODBC_SOURCE_DIR}/DriverManager/SQLTransact.c

${ODBC_SOURCE_DIR}/odbcinst/_logging.c
${ODBC_SOURCE_DIR}/odbcinst/_odbcinst_ConfigModeINI.c
${ODBC_SOURCE_DIR}/odbcinst/ODBCINSTConstructProperties.c
${ODBC_SOURCE_DIR}/odbcinst/ODBCINSTDestructProperties.c
${ODBC_SOURCE_DIR}/odbcinst/_odbcinst_GetEntries.c
${ODBC_SOURCE_DIR}/odbcinst/_odbcinst_GetSections.c
${ODBC_SOURCE_DIR}/odbcinst/ODBCINSTSetProperty.c
${ODBC_SOURCE_DIR}/odbcinst/_odbcinst_SystemINI.c
${ODBC_SOURCE_DIR}/odbcinst/_odbcinst_UserINI.c
${ODBC_SOURCE_DIR}/odbcinst/ODBCINSTValidateProperties.c
${ODBC_SOURCE_DIR}/odbcinst/ODBCINSTValidateProperty.c
${ODBC_SOURCE_DIR}/odbcinst/SQLConfigDataSource.c
${ODBC_SOURCE_DIR}/odbcinst/SQLConfigDriver.c
${ODBC_SOURCE_DIR}/odbcinst/SQLCreateDataSource.c
${ODBC_SOURCE_DIR}/odbcinst/_SQLDriverConnectPrompt.c
${ODBC_SOURCE_DIR}/odbcinst/SQLGetAvailableDrivers.c
${ODBC_SOURCE_DIR}/odbcinst/SQLGetConfigMode.c
${ODBC_SOURCE_DIR}/odbcinst/_SQLGetInstalledDrivers.c
${ODBC_SOURCE_DIR}/odbcinst/SQLGetInstalledDrivers.c
${ODBC_SOURCE_DIR}/odbcinst/SQLGetPrivateProfileString.c
${ODBC_SOURCE_DIR}/odbcinst/SQLGetTranslator.c
${ODBC_SOURCE_DIR}/odbcinst/SQLInstallDriverEx.c
${ODBC_SOURCE_DIR}/odbcinst/SQLInstallDriverManager.c
${ODBC_SOURCE_DIR}/odbcinst/SQLInstallerError.c
${ODBC_SOURCE_DIR}/odbcinst/SQLInstallODBC.c
${ODBC_SOURCE_DIR}/odbcinst/SQLInstallTranslatorEx.c
${ODBC_SOURCE_DIR}/odbcinst/SQLManageDataSources.c
${ODBC_SOURCE_DIR}/odbcinst/SQLPostInstallerError.c
${ODBC_SOURCE_DIR}/odbcinst/SQLReadFileDSN.c
${ODBC_SOURCE_DIR}/odbcinst/SQLRemoveDriver.c
${ODBC_SOURCE_DIR}/odbcinst/SQLRemoveDriverManager.c
${ODBC_SOURCE_DIR}/odbcinst/SQLRemoveDSNFromIni.c
${ODBC_SOURCE_DIR}/odbcinst/SQLRemoveTranslator.c
${ODBC_SOURCE_DIR}/odbcinst/SQLSetConfigMode.c
${ODBC_SOURCE_DIR}/odbcinst/SQLValidDSN.c
${ODBC_SOURCE_DIR}/odbcinst/SQLWriteDSNToIni.c
${ODBC_SOURCE_DIR}/odbcinst/SQLWriteFileDSN.c
${ODBC_SOURCE_DIR}/odbcinst/_SQLWriteInstalledDrivers.c
${ODBC_SOURCE_DIR}/odbcinst/SQLWritePrivateProfileString.c

${ODBC_SOURCE_DIR}/ini/iniAllTrim.c
${ODBC_SOURCE_DIR}/ini/iniAppend.c
${ODBC_SOURCE_DIR}/ini/iniClose.c
${ODBC_SOURCE_DIR}/ini/iniCommit.c
${ODBC_SOURCE_DIR}/ini/iniCursor.c
${ODBC_SOURCE_DIR}/ini/iniDelete.c
${ODBC_SOURCE_DIR}/ini/_iniDump.c
${ODBC_SOURCE_DIR}/ini/iniElement.c
${ODBC_SOURCE_DIR}/ini/iniElementCount.c
${ODBC_SOURCE_DIR}/ini/iniGetBookmark.c
${ODBC_SOURCE_DIR}/ini/iniGotoBookmark.c
${ODBC_SOURCE_DIR}/ini/iniObject.c
${ODBC_SOURCE_DIR}/ini/iniObjectDelete.c
${ODBC_SOURCE_DIR}/ini/iniObjectEOL.c
${ODBC_SOURCE_DIR}/ini/iniObjectFirst.c
${ODBC_SOURCE_DIR}/ini/iniObjectInsert.c
${ODBC_SOURCE_DIR}/ini/iniObjectLast.c
${ODBC_SOURCE_DIR}/ini/iniObjectNext.c
${ODBC_SOURCE_DIR}/ini/_iniObjectRead.c
${ODBC_SOURCE_DIR}/ini/iniObjectSeek.c
${ODBC_SOURCE_DIR}/ini/iniObjectSeekSure.c
${ODBC_SOURCE_DIR}/ini/iniObjectUpdate.c
${ODBC_SOURCE_DIR}/ini/iniOpen.c
${ODBC_SOURCE_DIR}/ini/iniProperty.c
${ODBC_SOURCE_DIR}/ini/iniPropertyDelete.c
${ODBC_SOURCE_DIR}/ini/iniPropertyEOL.c
${ODBC_SOURCE_DIR}/ini/iniPropertyFirst.c
${ODBC_SOURCE_DIR}/ini/iniPropertyInsert.c
${ODBC_SOURCE_DIR}/ini/iniPropertyLast.c
${ODBC_SOURCE_DIR}/ini/iniPropertyNext.c
${ODBC_SOURCE_DIR}/ini/_iniPropertyRead.c
${ODBC_SOURCE_DIR}/ini/iniPropertySeek.c
${ODBC_SOURCE_DIR}/ini/iniPropertySeekSure.c
${ODBC_SOURCE_DIR}/ini/iniPropertyUpdate.c
${ODBC_SOURCE_DIR}/ini/iniPropertyValue.c
${ODBC_SOURCE_DIR}/ini/_iniScanUntilObject.c
${ODBC_SOURCE_DIR}/ini/iniToUpper.c
${ODBC_SOURCE_DIR}/ini/iniValue.c

${ODBC_SOURCE_DIR}/log/logClear.c
${ODBC_SOURCE_DIR}/log/logClose.c
${ODBC_SOURCE_DIR}/log/_logFreeMsg.c
${ODBC_SOURCE_DIR}/log/logOn.c
${ODBC_SOURCE_DIR}/log/logOpen.c
${ODBC_SOURCE_DIR}/log/logPeekMsg.c
${ODBC_SOURCE_DIR}/log/logPopMsg.c
${ODBC_SOURCE_DIR}/log/logPushMsg.c

${ODBC_SOURCE_DIR}/lst/_lstAdjustCurrent.c
${ODBC_SOURCE_DIR}/lst/lstAppend.c
${ODBC_SOURCE_DIR}/lst/lstClose.c
${ODBC_SOURCE_DIR}/lst/lstDelete.c
${ODBC_SOURCE_DIR}/lst/_lstDump.c
${ODBC_SOURCE_DIR}/lst/lstEOL.c
${ODBC_SOURCE_DIR}/lst/lstFirst.c
${ODBC_SOURCE_DIR}/lst/_lstFreeItem.c
${ODBC_SOURCE_DIR}/lst/lstGetBookMark.c
${ODBC_SOURCE_DIR}/lst/lstGet.c
${ODBC_SOURCE_DIR}/lst/lstGotoBookMark.c
${ODBC_SOURCE_DIR}/lst/lstGoto.c
${ODBC_SOURCE_DIR}/lst/lstInsert.c
${ODBC_SOURCE_DIR}/lst/lstLast.c
${ODBC_SOURCE_DIR}/lst/lstNext.c
${ODBC_SOURCE_DIR}/lst/_lstNextValidItem.c
${ODBC_SOURCE_DIR}/lst/lstOpen.c
${ODBC_SOURCE_DIR}/lst/lstOpenCursor.c
${ODBC_SOURCE_DIR}/lst/lstPrev.c
${ODBC_SOURCE_DIR}/lst/_lstPrevValidItem.c
${ODBC_SOURCE_DIR}/lst/lstSeek.c
${ODBC_SOURCE_DIR}/lst/lstSeekItem.c
${ODBC_SOURCE_DIR}/lst/lstSet.c
${ODBC_SOURCE_DIR}/lst/lstSetFreeFunc.c
${ODBC_SOURCE_DIR}/lst/_lstVisible.c
)

add_library(unixodbc ${LINK_MODE} ${SRCS})

target_link_libraries(unixodbc ltdl)

# SYSTEM_FILE_PATH was changed to /etc

target_include_directories(unixodbc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/linux_x86_64/private)
target_include_directories(unixodbc PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/linux_x86_64)
target_include_directories(unixodbc PUBLIC ${ODBC_SOURCE_DIR}/include)

target_compile_definitions(unixodbc PRIVATE -DHAVE_CONFIG_H)

target_compile_options(unixodbc PRIVATE -Wno-dangling-else -Wno-parentheses -Wno-misleading-indentation -Wno-unknown-warning-option -O2)
